package com.bootcamp.mvc1125.config;

import com.bootcamp.mvc1125.converter.DateConverter;
import com.bootcamp.mvc1125.interceptor.HandlerInterceptor1;
import com.bootcamp.mvc1125.interceptor.HandlerInterceptor2;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * @program: spring-1125-mvc-java-config
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-25 11:18
 **/


/*
* 以 MVC开头标签的配置：
*   如：validator，interceptor，resources，convertors
*    实现的接口 可以实现上述配置
* */

@ComponentScan("com.bootcamp.mvc1125.controller")
@EnableWebMvc
public class SpringMvcConfiguration implements WebMvcConfigurer {

    /* mvc:interceptors -> add interceptor 方法*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor1());
        registry.addInterceptor(new HandlerInterceptor2()).addPathPatterns("/hello/**");
    }

    /* mvc:resources */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 不要忘记了 Location最后的 /
        registry.addResourceHandler("/pic1/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("/");
        registry.addResourceHandler("/pic3/**").addResourceLocations("file:d:/wd/pic/");
    }


    /*
    * 1. 取出 conversionService
    * 2. 实现自定义 converter,并用生命周期 init方法添加
    * 3. 重新放回组件。
    * */
    /*
    @Autowired
    ConfigurableConversionService conversionService;
    @PostConstruct
    public void addConverter(){
        conversionService.addConverter(new DateConverter());
    }
    @Primary
    @Bean
    public ConfigurableConversionService conversionService(){
        return conversionService;
    }
    */

    /* 方便的添加 Converter*/
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
    }

    /* 使用文件上传*/
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }

    /* 添加 validator*/
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setProviderClass(HibernateValidator.class);
        return validator;
    }


}
